package com.yxh.lib_base.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintSet
import com.gyf.immersionbar.ktx.statusBarHeight
import com.yxh.lib_base.databinding.AbsTitleLayoutBinding
import com.yxh.lib_base.databinding.ActivityBaseTitleBinding
import com.yxh.lib_base.ext.lazyNone
import com.yxh.lib_base.ext.nav
import com.yxh.lib_base.ext.view.clickNoRepeat
import com.yxh.lib_base.ext.view.visibleOrGone
import com.yxh.lib_base.ext.viewbinding.viewBinding

/**
 * @作者：yuan_xh
 * @时间：2022/8/25 19:40
 * @描述：
 **/
abstract class BaseTitleFragment : BaseFragment() {
    private val _Binding by viewBinding<ActivityBaseTitleBinding>()

    //是否是重叠标题栏
    open var isOverlapping = false

    protected val mTitleBar by lazyNone { _Binding.titleBar }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = _Binding.root
        isFirst = true
        onCreated(savedInstanceState)
        return root
    }

    override fun onCreated(savedInstanceState: Bundle?) {
        replaceContentWithView(_Binding.absContent, mBinding.root)
        if (isOverlapping) {
            setOverlappingTitleBar()
            initStatusBar(mTitleBar.root)
        } else {
            initStatusBar {
                statusBarDarkFont(true)
                _Binding.absRootLayout.setPadding(0, statusBarHeight, 0, 0)
            }
        }
    }

    /**
     *  <替换 View>
     *  <使 DataBinding 替换 ViewStub 的方案>
     *      ~~ 参考viewStub
     */
    private fun replaceContentWithView(viewOld: View, replaceView: View) {
        (viewOld.parent as? ViewGroup)?.let { parent ->
            val index = parent.indexOfChild(viewOld)
            val layoutParams: ViewGroup.LayoutParams = viewOld.layoutParams
            parent.removeViewInLayout(viewOld)
            if (layoutParams != null) {
                parent.addView(replaceView, index, layoutParams)
            } else {
                parent.addView(replaceView, index)
            }
        }
    }

    /**
     * 设置重叠的标题栏
     */
    private fun setOverlappingTitleBar() {
        val constraintSet = ConstraintSet()
        constraintSet.clone(_Binding.absContent)
        constraintSet.connect(
            _Binding.absRootLayout.id,
            ConstraintSet.TOP,
            ConstraintSet.PARENT_ID,
            ConstraintSet.TOP,
            0
        )
        constraintSet.applyTo(_Binding.absContent)
    }


    protected fun addTitleBar(
        isNav: Boolean = true,
        titleBar: (AbsTitleLayoutBinding.() -> Unit)? = null
    ) {
        mTitleBar.activityBaseBack.setOnClickListener {
            if (isNav) {
                nav().navigateUp()
            } else {
                activity?.finish()
            }
        }
        titleBar?.invoke(mTitleBar)
    }

    protected fun AbsTitleLayoutBinding.addTitleRightBtn(title: String, onClick: () -> Unit) {
        activityBaseRTv.visibleOrGone(!title.isNullOrEmpty())?.let {
            it.text = title
            it.clickNoRepeat {
                onClick.invoke()
            }
        }
    }

    /**
     * 初始化状态栏
     */
    open fun initStatusBar(view: View) {
        initStatusBar {
            statusBarDarkFont(true)
            titleBar(view)
        }
    }

}